/*
 * EK.repository: A global object repository to promote shareability
 * 
 * Enrichment Kit @ek.version@ (http://enrichmentkit.com)
 * Copyright (c) 2008 Pink Hominid
 * Licensed under terms of the MIT License (LICENSE.txt)
 * 
 * IE 6.0+, FF 1.5+ Tested
 */
EK.repository = new function EkRepository() {
    var cache = {};
	this.refCounter = {};
    
    this.factory = {
        get: function (type) {
            var ref;
            if ( typeof type == "function") { ref = new type(); }
            else { ref = type; }
            return ref;
        }       
    };
    
    this.locate = function (shareName, type) {
        var ref;
        if (shareName) {
            ref = cache[shareName];
            if (!ref) {
                ref = this.factory.get(type);
				if (typeof ref !== 'undefined' && ref!== null && typeof ref === 'object') {
					cache[shareName] = ref;
				}
            }
			this.refCounter[shareName] = this.refCounter[shareName] || 0;
			this.refCounter[shareName]++;
        }
        return ref;
    };
	
    this.free = function (shareName) {
        var ref;
        if (shareName) {
            ref = cache[shareName];
            if (ref) {
                this.refCounter[shareName]--;
                if (this.refCounter[shareName] <= 0) {
                    cache[shareName] = ref = undefined;
                    this.refCounter[shareName] = undefined;
                }
            }
        }
    };
}();
